<?php
/**
 * Created by PhpStorm.
 * User: Dark
 * Date: 2015/8/2
 * Time: 1:06
 */

namespace Home\Model;
use Think\Model\ViewModel;


class GoodsLotteryDetailInfoViewModel extends ViewModel {
    /*
     *定义视图
     */
    public $viewFields = array(
      'goods'=>array('ID','brandType_ID','goodsType_ID','goodsName','unitPrice','otherSpecifications','imageURLs','clickNumber','collectNumber','purchaseNumber','shelveNumber','currentNumber','_as'=>'a'),
      'goods_type'=>array('goodsTypeName','_as'=>'b','_on'=>'a.goodsType_ID=b.ID'),
      'brand_type'=>array('brandTypeName','_as'=>'c','_on'=>'a.brandType_ID=c.ID'),
      'lottery_goods_info'=>array('school_ID','user_ID','luckyNumber','userNickName','totalNumber','startDate','lotteryDate','lotteryTurn','priceLevel','_as'=>'d','_on'=>'a.ID=d.goods_ID')
    );

    /*
     * 给视图添加一条信息
     */
    public function addInfo($data) {
        if(!empty($data)) {
            $this->create();
            return $this->add(data);
        } else {
            return false;
        }
    }

    /*
     * 修改一条信息根据拼购商品信息
     */
    public function updateByInfo($good,$data) {
        if(!empty($good)) {
            if(!empty($data)) {
                return $this->where('ID='.$good['id'].' and lotteryTurn='.$good['lotteryTurn'].' and priceLevel='.$good['priceLevel'])->save($data);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /*
     * 查询视图所有结果
     */
    public function queryAll() {
        return $this->order('startDate desc, lotteryTurn desc, ID desc')->select();
    }

    /*
     * 查询视图按照价格
     */
    public function queryByPrice($price) {
        if(!empty($price)) {
            return $this->where('priceLevel='.$price)->select();
        }
    }

    /*
     * 查询视图按照品牌
     */
    public function queryByBrand($brand) {
        if(!empty($brand)) {
            return $this->where('brandType_ID='.$brand.' or brandTypeName='.$brand)->select();
        } else {
            return false;
        }
    }

    /*
     * 查询视图按照分类
     */
    public function queryByType($type) {
        if(!empty($type)) {
            return $this->where('goodsType_ID='.$type.' or goodsTypeName='.$type)->select();
        } else {
            return false;
        }
    }

    /*
     * 删除信息根据拼购商品信息
     */
    public function deleteByInfo($good) {
        if(!empty($good)) {
            return $this->where('ID='.$good['id'].' and lotteryTurn='.$good['lotteryTurn'].' and priceLevel='.$good['priceLevel'])->delete();
        } else {
            return false;
        }
    }
}